package com.ayoka.parenting.ui.meditation.question;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.MeditationQuestionBean;
import com.ayoka.parenting.bean.ResponseMeditationQuestion;
import com.ayoka.parenting.databinding.ActivityMeditationQuestionBinding;
import com.ayoka.parenting.mvp.MVPBaseActivity;
import com.jincp.common.function.FunctionManager;
import com.jincp.common.function.FunctionNoParamNoResult;
import com.jincp.common.model.ToolBarOptions;
import com.jincp.common.utils.MyLogger;

import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: MeditationQuestionActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class MeditationQuestionActivity extends MVPBaseActivity<ActivityMeditationQuestionBinding, MeditationQuestionContract.View, MeditationQuestionPresenter> implements MeditationQuestionContract.View {

    public static void start(Activity activity, String recordId) {
        Intent intent = new Intent(activity, MeditationQuestionActivity.class);
        intent.putExtra("RecordId", recordId);
        activity.startActivity(intent);
    }

    private String recordId = "140";

    private ResponseMeditationQuestion questionBean;

    private int questionIndex;

    @Override
    protected void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null) {
            recordId = intent.getStringExtra("RecordId");
        }
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_meditation_question;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions();
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        if (mPresenter != null) {
            mPresenter.getMeditationQuestionData(recordId);
        }
        FunctionManager.getInstance().addFunction(new FunctionNoParamNoResult("QuestionStart") {
            @Override
            public void invoke() {
                showNextQuestionFragment(0);
            }
        });
        FunctionManager.getInstance().addFunction(new FunctionNoParamNoResult("QuestionNext") {
            @Override
            public void invoke() {
                showNextQuestionFragment(++questionIndex);
            }
        });
        FunctionManager.getInstance().addFunction(new FunctionNoParamNoResult("QuestionPrevious") {
            @Override
            public void invoke() {
                --questionIndex;
                showPreviousQuestionFragment();
            }
        });
        showStartFragment();
    }


    @Override
    protected MeditationQuestionPresenter getPresenter() {
        return new MeditationQuestionPresenter();
    }

    @Override
    public void configMeditationQuestionData(ResponseMeditationQuestion data) {
        if (data == null) return;
        this.questionBean = data;
    }

    @Override
    public void onSaveQuestionAnswerCallback(boolean isSuccess) {
        if (isSuccess) {
            if (mPresenter != null) {
                mPresenter.showSynchronizedCircleGroupDialog(getSupportFragmentManager(), questionBean);
            }
        }
    }

    @Override
    public void onSynchronizedCircleGroupCallback(boolean isSuccess) {
        if (mPresenter != null && questionBean != null && questionBean.meditation != null) {
            mPresenter.showMeditationCompletedDialog(getSupportFragmentManager(), questionBean.meditation.OrderNum);
        }
    }

    @Override
    public void onDialogDismiss(DialogFragment dialog) {
        if (dialog instanceof MeditationCompletedDialog){
            finish();
        }
    }


    private void showStartFragment() {
        Fragment startFragment = MeditationQuestionStartFragment.newInstance();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.frameLayout, startFragment).commitAllowingStateLoss();
    }

    private void showNextQuestionFragment(int index) {
        if (questionBean != null && questionBean.questions != null) {
            if (index < 0) {
                MyLogger.d(TAG, "first");
            } else if (index > questionBean.questions.size() - 1) {
                MyLogger.d(TAG, "last");
                if (mPresenter != null) {
                    mPresenter.saveQuestionAnswer(recordId, questionBean);
                }
            } else {
                this.questionIndex = index;
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.setCustomAnimations(
                        R.anim.slide_right_in,
                        R.anim.slide_left_out,
                        R.anim.slide_left_in,
                        R.anim.slide_right_out);
                Fragment fragmentById = fragmentManager.findFragmentById(R.id.frameLayout);
                if (fragmentById != null) {
                    transaction.hide(fragmentById);
                }
                MeditationQuestionBean question = questionBean.questions.get(questionIndex);
                question.Answer = new MeditationQuestionBean.AnswerResultBean(question.ID, question.QuestionType);
                Fragment fragment = MeditationQuestionDetailFragment.newInstance(question, questionIndex == 0, questionIndex == questionBean.questions.size() - 1);
                transaction.add(R.id.frameLayout, fragment);
                transaction.addToBackStack(null);
                transaction.commitAllowingStateLoss();
            }
        }
    }

    private void showPreviousQuestionFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        if (fragmentManager.getBackStackEntryCount() > 1) {
            fragmentManager.popBackStack();
        }
    }

}
